package com.binarysoft.service.user;

import java.util.Collection;

import org.apache.ibatis.session.SqlSession;

import com.binarysoft.service.id.IDService;
import com.github.pagehelper.PageHelper;
import common.framework.dsb.AbstractServiceBean;
import common.framework.dsb.annotation.DSBLog;
import common.framework.dsb.annotation.DSBService;
import common.framework.dsb.annotation.DSBSql;
import common.framework.dsb.service.ServiceContext;

@DSBService(name = "DSB/UserService", startup = true)
public class UserServiceBean extends AbstractServiceBean implements UserService {

	@DSBLog(log = "console")
	@Override
	public void start(ServiceContext serviceContext) throws Exception {
		super.start(serviceContext);
	}

	@Override
	public void refresh() throws Exception {
	}

	@Override
	public void close() throws Exception {
	}

	public void before() throws Exception {
	}

	public void after() {
	}

	@DSBLog(log = "console")
	@DSBSql(environment = "mysql_dev")
	@Override
	public Collection<UserInfo> getAllUsers() throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		UserInfoDAO dao = sqlSession.getMapper(UserInfoDAO.class);
		return dao.selectAll();
	}

	@DSBLog(log = "console")
	@DSBSql(environment = "mysql_dev")
	@Override
	public Collection<UserInfo> getAllUsers(int pageNum, int pageSize) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		UserInfoDAO dao = sqlSession.getMapper(UserInfoDAO.class);
		PageHelper.startPage(pageNum, pageSize);
		return dao.selectAll();

	}

	@DSBSql(environment = "mysql_dev")
	@Override
	public Collection<UserRole> getAllRoles() throws Exception {
		return null;
	}

	@DSBLog(log = "console")
	@DSBSql(environment = "mysql_dev")
	@Override
	public UserInfo getUserByID(long id) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		UserInfoMapper dao = sqlSession.getMapper(UserInfoMapper.class);
		return dao.selectByPrimaryKey(id);
	}

	@DSBSql(environment = "mysql_dev")
	@Override
	public UserInfo geUserByLoginID(String loginID) throws Exception {
		return null;
	}

	@DSBSql(environment = "mysql_dev")
	@Override
	public long createUser(UserInfo user) throws Exception {
		
		IDService idService = this.serviceContext.lookup("DSB/IDService");
		user.setId(idService.nextID());
		
		SqlSession sqlSession = this.getSqlSession();
		UserInfoDAO dao = sqlSession.getMapper(UserInfoDAO.class);
		dao.insert(user);
		return user.getId();
	}

	@DSBSql(environment = "mysql_dev")
	@Override
	public void deleteUser(long id) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		UserInfoDAO dao = sqlSession.getMapper(UserInfoDAO.class);
		dao.deleteByPrimaryKey(id);
	}

	@Override
	public void setUserDeletedStatus(long id, boolean deleted) throws Exception {

	}

	@Override
	public void setUserStatus(long id, boolean active) throws Exception {
	}

	@Override
	public void updateUser(long id, UserInfo user) throws Exception {
	}

	@Override
	public boolean changePass(long id, String oldpass, String newpass) throws Exception {
		return false;
	}

	@Override
	public void resetPass(long id, String newpass) throws Exception {

	}

	@Override
	public UserRole getRoleByID(String id) throws Exception {
		return null;
	}

	@Override
	public void createRole(UserRole role) throws Exception {

	}

	@Override
	public void deleteRole(String id) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void updateRole(String id, UserRole role) throws Exception {
		// TODO Auto-generated method stub
	}
}
